package com.mifan.mapper;

import com.mifan.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mifan.vo.UserListVo;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author Milan
 * @since 2024-09-13
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户名获取后台用户
     */
    @Select("select *, ums_role.name as rolename, ums_role.description as roledesc from ums_user left join ums_role on ums_user.roleid = ums_role.id where username=#{username}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "nickname", property = "nickname"),
            @Result(column = "roleid", property = "roleid"),
            @Result(column = "status", property = "status"),
            @Result(column = "created", property = "created"),
            @Result(column = "updated", property = "updated"),
            @Result(column = "rolename", property = "roleName"),
            @Result(column = "roledesc", property = "roleDesc"),
    })
    UserListVo getUserByUsername(String username);


    /**
     * 获取所有用户及其角色
     *
     * @return
     */
    @Select("select u.*, ur.name AS role_name,  ur.description AS role_desc from ums_user u left join ums_role ur on u.roleid = ur.id where u.id != 1")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "nickname", property = "nickname"),
            @Result(column = "roleid", property = "roleid"),
            @Result(column = "status", property = "status"),
            @Result(column = "created", property = "created"),
            @Result(column = "updated", property = "updated"),
            @Result(column = "role_name", property = "roleName"),
            @Result(column = "role_desc", property = "roleDesc"),

    })
    List<UserListVo> getUsersWithRoles();
}
